﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Net.Mail;
using System.Net;

using VIT.DataHelper.DataAccess;
using VIT.Library.Web;
using VIT.Business;
using VIT.Wedding;

namespace VIT.Web
{
    public partial class Contact : VIT.Web.UI.VITComponent
    {
        private string vId
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("sId")) ? "ContactPage" : Page.Request.Params.Get("sId"); }
        }
        public BLPage page;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Create a random code and store it in the Session object.
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }

            page = new BLPage(vId, "vi-VN");
            page.GetSingle();

            loadCaptChaImage();
            //loadLangForControl();
            string a = URLTemplatePath;
        }

        //private void loadLangForControl()
        //{
        //    btnDoiMa.Text = (string)GetLocalResourceObject("ChangeCode");
        //    btnSend.Text = (string)GetLocalResourceObject("Send");
        //    btnExit.Text = (string)GetLocalResourceObject("ReWrite");
        //}

        private void loadCaptChaImage()
        {
            HatchBrush HatchBrushAnh = new HatchBrush(HatchStyle.HorizontalBrick, Color.White, Color.White);
            HatchBrush HatchBrusText = new HatchBrush(HatchStyle.ZigZag, Color.Blue, Color.Blue);
            if (Session["CaptchaImageText"] == null) this.Session["CaptchaImageText"] = GenerateRandomCode();
            imgCaptcha.Src = MainCore.captChaImage(Session["CaptchaImageText"].ToString(), HatchBrushAnh, HatchBrusText);
        }

        protected void btnDoiMa_Click(object sender, EventArgs e)
        {
            this.Session["CaptchaImageText"] = GenerateRandomCode();
            loadCaptChaImage();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            // kiem tra ma xac nhan
            string code = Session["CaptchaImageText"].ToString();
            if (txtContent.Text.Length > 0)
            {
                if (txtCode.Text != code)
                {
                    setTrueVisible(lblMaXacNhan);
                    this.Session["CaptchaImageText"] = GenerateRandomCode();
                    loadCaptChaImage();
                }
                else
                {
                    // gui mail
                    //BLConfig c = new BLConfig(); c.LanguageId = Session["language"].ToString(); c.GetConfig();
                    //string send = MainCore.sendemail(c.Email, txtEmail.Text, "Liên hệ", inforcus(), "smtp.gmail.com", "smtp.gmail.com");
                    //if (send.Length > 0) setTrueVisible(lblThanhCong);
                    //else setTrueVisible(lblThatBai);
                    int vId = 1;// int.Parse(Request["sId"]);
                    var company = VITData.GetObject<CompanyDetail>(o => o.Id == vId);
                    if (company != null)
                    {
                        SmtpClient mailClient = new SmtpClient(company.MailHost, 587);
                        mailClient.EnableSsl = true;
                        NetworkCredential cred = new NetworkCredential(company.Email, company.Pass);
                        mailClient.Credentials = cred;
                        MailMessage mail = new MailMessage(txtEmail.Text, company.Email, "Liên hệ", inforcus());
                        mail.IsBodyHtml = true;
                        mailClient.Send(mail);

                        setTrueVisible(lblThanhCong);
                        this.Session["CaptchaImageText"] = GenerateRandomCode();
                        loadCaptChaImage();
                    }
                    else setTrueVisible(lblThatBai);
                }
            }
            else setTrueVisible(lblNoiDung);
        }

        //protected void btnExit_Click(object sender, EventArgs e)
        //{
        //    txtName.Text = "";
        //    txtPhone.Text = "";
        //    txtAddress.Text = "";
        //    txtContent.Text = "";
        //    txtSubject.Text = "Góp ý";
        //}

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblMaXacNhan.Visible = false;
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblThongBao.Visible = false;

            control.Visible = true;
        }

        private string inforcus()
        {
            string chuoi = "<center><table>";
            chuoi += "<tr><td style='width:200px'>Tên:</td><td style='width:500px'>" + ddlWho.SelectedItem.Text + ": " + txtFirstName.Text + " " + txtLastName.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Email:</td><td style='width:500px'>" + txtEmail.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Điện thoại:</td><td style='width:500px'>" + txtPhone.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Nội dung:</td><td style='width:500px'>" + txtContent.Text + "</td></tr>";
            chuoi += "</table><p /><hr /><a href='" + URLRoot + "'>" + URLRoot + "</a></center>";
            return chuoi;
        }

        private string GenerateRandomCode()
        {
            Random random = new Random();
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
